/**
 * @author kristian
 */
function mousePos(ev){
    var x, y;
    
    // Get the mouse position relative to the canvas element.
    if (ev.layerX || ev.layerX == 0) { // Firefox
        x = ev.layerX;
        y = ev.layerY;
    }
    else 
        if (ev.offsetX || ev.offsetX == 0) { // Opera
            x = ev.offsetX;
            y = ev.offsetY;
        }
    return {
        x: x,
        y: y
    };
}


var sameHex = function(hex1, hex2) {
	if (hex1 && hex2) 
		return (hex1.i === hex2.i) && (hex1.j === hex2.j);
	else {
		return false;
	}
}

var sameHex2 = function(hex1, hex2) {
	if (hex1 && hex2)  {
		// console.log(hex1);
		// console.log(hex2);
		return ((hex1.i === hex2.i) && (hex1.j === hex2.j));
	}
	else {
		console.log('hex is null');
		console.log(hex1);
		console.log(hex2);
		return false;
	}
}

var action;

var actions = [];

var undo = function() {
}

var doAction = function(action){
	var options = {
		scale: {
			x: 60,
			y: 70
		}
	};
	drawUsingTextureSelected = action.drawUsingTextureSelected || false;
	drawUsingDecorSelected = action.drawUsingDecorSelected || false;
	drawUsingPathSelected = action.drawUsingPathSelected || false;
	doHexAction(action.hex, options)
}

var doHexAction = function(hex, options){
	currentHex.i = hex.i;
	currentHex.j = hex.j;
	options.hex = hex;
	action = {
		hex: hex
	};
	if (terrainSelected) {
		drawUsingTextureSelected = true;
		drawUsingDecorSelected = false;
		drawUsingPathSelected = false;
		action.drawUsingTextureSelected = drawUsingTextureSelected;
	}
	else 
		if (decorSelected) {
			drawUsingTextureSelected = false;
			drawUsingPathSelected = false;
			drawUsingDecorSelected = true;
			action.drawUsingDecorSelected = drawUsingDecorSelected;
		}
		else 
			if (pathSelected) {
				drawUsingTextureSelected = false;
				drawUsingPathSelected = true;
				drawUsingDecorSelected = false;
				action.drawUsingPathSelected = drawUsingPathSelected;
			}
	// console.log('drawTextureUsingHex');
	
	
	actions.push(action);
	drawTextureUsingHex(ctx, options);
	drawTextureOverlays(ctx);
}

var isValidCoords = function (hexCoords) {
	return !isNaN(hexCoords.x) && !isNaN(hexCoords.y);
}

var maybeDrawHexTexture = function(tool, ev){
    var options = {
        scale: {
			x: 60,
			y: 70
		}
    };
	// var hexCoords = oldCalcHex(ev, options);	
	var mousePoint = {x: ev._x -10, y: ev._y - 10};
	
	var hexCoords = calcHexCoordsFromMouseCoords(mousePoint, options.scale);	
	var hexX = hexCoords.x;
	var hexY = hexCoords.y;
	console.log('Hex point clicked: x=' + hexCoords.x + ', y=' + hexCoords.y);
	if (isValidCoords(hexCoords)) {
	    try {		
		    var point = new HEX.Point(hexX, hexY);
			console.log('Hex point: x=' + point.x + ', y=' + point.y);
			var hex = new HEX.Hex(textureGrid, hexX, hexY);
			// var hex = textureGrid.hexPoint(point);
			// hex.i =hexX;
			// hex.j = hexY;
	        tool.started = true;
	        if (!sameHex2(hex, currentHex)) {
				doHexAction(hex, options);
	        } // else console.log('NOT drawTextureUsingHex');
	    } 
	    catch (e) {
			console.log(e);
	    }
    } else console.log('Hex coordinates could not be calculated');
}

var currentHex = {};

// This painting tool works like a drawing pencil which tracks the mouse 
// movements.
function tool_pencil(){
    var tool = this;
    this.started = false;
    
    // This is called when you start holding down the mouse button.
    // This starts the pencil drawing.
    this.mousedown = function(ev){
		if (terrainSelected)
	        maybeDrawHexTexture(tool, ev);
		if (decorSelected)
			maybeDrawHexTexture(tool, ev);
		if (pathSelected)
			maybeDrawHexTexture(tool, ev);

    };
    
    // This function is called every time you move the mouse. Obviously, it only 
    // draws if the tool.started state is set to true (when you are holding down 
    // the mouse button).
    this.mousemove = function(ev){
        if (tool.started) {
            maybeDrawHexTexture(tool, ev);
        }
        
    };
    
    // This is called when you release the mouse button.
    this.mouseup = function(ev){
        if (tool.started) {
            tool.mousemove(ev);
            tool.started = false;
			currentHex = {};
        }
    };
}


// The general-purpose event handler. This function just determines the mouse 
// position relative to the canvas element.
function ev_canvas(event){
    var ev = event.originalEvent;
    if (ev.layerX || ev.layerX == 0) { // Firefox
        ev._x = ev.layerX;
        ev._y = ev.layerY;
    }
    else 
        if (ev.offsetX || ev.offsetX == 0) { // Opera
            ev._x = ev.offsetX;
            ev._y = ev.offsetY;
        }
    // console.log(ev._x + ',' + ev._y);
    // Call the event handler of the tool.
    var func = tool[ev.type];
    if (func) {
        func(ev);
    }
    else 
        console.log('no mouse function found for current tool');
}
